<?php
// +----------------------------------------------------------------------
// | Author: July
// +----------------------------------------------------------------------
// | 创建时间: 2023-12-27 20:39:42
// +----------------------------------------------------------------------
// | 配置模型
// +----------------------------------------------------------------------

namespace july\julyadmin\common\model;

use think\Model;

class Config extends Model
{
    //主键
    protected $pk = 'id';
    //设置自动时间写入类型
    protected $autoWriteTimestamp = 'datetime';
    //设置创建时间字段
    protected $createTime = 'create_time';
    //设置更新时间字段
    protected $updateTime = 'update_time';
    //设置删除时间字段
    protected $deleteTime = false;


    /**
     * 获取配置值
     * @Author   July
     * @DateTime 2024-01-13T20:17:55+0800
     * @param  string $key 配置key，如sms_config，sms_config.appid
     * @return config
     */
    public static function config($key = '')
    {
        if (!$key) {
            throw new \Exception("配置" . $key . "不能为空");
        }
        $keys = explode('.', $key);
        $find = self::where(['config_key' => $keys[0]])->cache('web_config_' . $keys[0])->find();
        if (!$find) {
            throw new \Exception("配置不存在！");
        }
        $find   = $find->toArray();
        $config = json_decode($find['config'], true);
        $value  = '';
        foreach ($keys as $config_key => $config_v) {
            if ($config_key == 0) {
                $value = $config;
            } else {
                if (!isset($value[$config_v])) {
                    $value = '';
                    // throw new \Exception("配置" . $key . "不存在");
                } else {
                    $value = $value[$config_v];
                }
            }
        }
        return $value;
    }
}
